<template>
  <el-dialog title="添加用户" :visible.sync="dialog" @close="closeDialog">
    <el-form class="form-box" ref="loginForm" :rules="addUserRules" :model="addUserForm" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="addUserForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="addUserForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="addUserForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="addUserForm.mobile"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialog = false">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data () {
    var testEmail = (rule, val, cb) => {
      const reqEmail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
      if (reqEmail.test(val)) {
        return cb()
      } else {
        cb(new Error('邮箱格式不正确'))
      }
    }
    var testPhone = (rule, val, cb) => {
      const reqPhone = /^1[3456789]\d{9}$/
      if (reqPhone.test(val)) {
        return cb()
      } else {
        cb(new Error('手机号不正确'))
      }
    }
    return {
      dialog: false,
      addUserForm: {
        username: '',
        password: '123456',
        email: 'a@q.com',
        mobile: '13112341234'
      },
      // 输入规则验证
      addUserRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 1   5 个字符',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: testEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: testPhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹窗打开
    openDialog (data) {
      console.log(data)

      this.dialog = data[0]
    }, // 弹窗关闭
    closeDialog () {
      this.$refs.loginForm.resetFields()
    },
    // 添加用户
    add () {
      this.$refs.loginForm.validate(async status => {
        console.log(status)
        if (!status) {
          return this.$message.error('请填写正确信息')
        }
        console.log(this.addUserForm)

        const { data: res } = await this.axios.post('users', this.addUserForm)
        console.log(res)
        if (res.meta.status !== 201) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          // 关闭弹窗
          this.dialog = false
          // 重新获取数据
          this.$parent.getUserList()
        }
      })
    }
  }
}
</script>
